package com.dpiinc.TechPage

import android.content.Intent
import com.dpiinc.TechPage.room.RoomAiWriterDatabase
import com.dpiinc.TechPage.room.bean.UserEntity

/**
 * User管理类
 * Created by X on 2018/7/8.
 */
object UserManager {

    fun getAccessToken(): String {
        val user = getUser()
        return if (user.accessToken.isNullOrEmpty()) {
            goLogin()
            ""
        } else {
            user.accessToken
        }
    }

    fun getUserId(): String {
        val user = getUser()
        return if (user.uid.isNullOrEmpty()) {
            goLogin()
            ""
        } else {
            user.uid
        }
    }

    fun getUser(): UserEntity {
        val db = RoomAiWriterDatabase.getInstance(AIWriteApplication.instance())
        val userList = db.userDao().all
        if (userList != null && userList.isNotEmpty()) {
            return userList[0]
        } else {
            goLogin()
            return UserEntity()
        }

    }

    private fun goLogin() {
//        val intent = Intent(AIWriteApplication.instance(), LoginActivity::class.java)
//        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//        AIWriteApplication.instance().startActivity(intent)
    }
}